package org.linlinjava.litemall.admin.service;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.ProgressListener;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.LitemallStorageService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class AdminMinioStroageService extends ResponseUtil {

    private final FileStorageService fileStorageService;
    private final LitemallStorageService litemallStorageService;

    public Response upload(MultipartFile multipartfile) {
        FileInfo fileInfo = fileStorageService.of(multipartfile)
                .image(img -> img.size(1000, 1000))  // 将图片大小调整到 1000*1000
                //.thumbnail(th -> th.size(500, 500))  // 再生成一张 200*200 的缩略图
                .upload();
        //保存对象到数据库 把原图和缩略图一起保存
        //上传的原图
        LitemallStorage storageOriginalInfo = new LitemallStorage();
        storageOriginalInfo.setName(fileInfo.getOriginalFilename());
        String s = String.valueOf(fileInfo.getSize());
        storageOriginalInfo.setSize(Integer.valueOf(s));
        storageOriginalInfo.setType(fileInfo.getContentType());
        storageOriginalInfo.setKey(fileInfo.getFilename());
        storageOriginalInfo.setUrl(fileInfo.getUrl());
        storageOriginalInfo.setAddTime(LocalDateTime.now());
        storageOriginalInfo.setUpdateTime(LocalDateTime.now());
        storageOriginalInfo.setDeleted(false);
        //生成的缩略图
        /*LitemallStorage storageInfo = new LitemallStorage();
        storageInfo.setName(fileInfo.getOriginalFilename());
        String ss = String.valueOf(fileInfo.getThSize());
        storageInfo.setSize(Integer.valueOf(ss));
        storageInfo.setType(fileInfo.getThContentType());
        storageInfo.setKey(fileInfo.getThFilename());
        storageInfo.setUrl(fileInfo.getThUrl());
        storageInfo.setAddTime(LocalDateTime.now());
        storageInfo.setUpdateTime(LocalDateTime.now());
        storageInfo.setDeleted(false);*/
        //批量存储
        litemallStorageService.saveBatch(Arrays.asList(storageOriginalInfo));
        return fileInfo == null?fail("上传失败！"):success(storageOriginalInfo);
    }


    public Response download(String filePath) {
        // 获取文件信息
        FileInfo fileInfo = fileStorageService.getFileInfoByUrl(filePath);

        // 方式三
        fileStorageService.download(filePath).setProgressMonitor(new ProgressListener() {
            @Override
            public void start() {
                System.out.println("下载开始");
            }
            @Override
            public void progress(long progressSize,long allSize) {
                System.out.println("已下载 " + progressSize + " 总大小" + allSize);
            }
            @Override
            public void finish() {
                System.out.println("下载结束");
            }
        }).file("C:\\"+fileInfo.getFilename());
        return success(fileInfo);
    }

    public Response delete(String filePath) {
        //获取文件信息
        FileInfo fileInfo = fileStorageService.getFileInfoByUrl(filePath);
        //判断文件是否存在
        boolean exists = fileStorageService.exists(fileInfo);
        if(exists){
            //直接删除
            fileStorageService.delete(fileInfo);
        }
        return success(fileInfo);
    }

}
